package com.slkj.shilixiaoyuanapp.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.slkj.shilixiaoyuanapp.R;

/**
 * Created  on 2018-07-31
 *
 * @author 唐开阔
 * @describe 多状态布局
 */
public class StateLayout extends FrameLayout {
    public static final int CONTENT = 1;
    public static final int LOADING = 2;
    public static final int EMPTY = 3;
    public static final int ERROR = 4;
    //不同状态下的提示文字
    private String loadingStr, emptyStr, errorStr;
    //当前的页面状态
    private int nowState;
    private View contentView;
    private View emptyView;
    private View loadingView;
    private View errorView;
    OnReLoadListener onReLoadListener;
    public StateLayout(@NonNull Context context) {
        this(context, null);
    }

    public StateLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StateLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StateLayout);
        loadingStr = typedArray.getString(R.styleable.StateLayout_loadingStr);
        emptyStr = typedArray.getString(R.styleable.StateLayout_emptyStr);
        errorStr = typedArray.getString(R.styleable.StateLayout_errorStr);
        nowState = typedArray.getInt(R.styleable.StateLayout_init_type, CONTENT);
        if (isInEditMode()){
            nowState = CONTENT;
        }
        typedArray.recycle();
    }

    public void setOnReLoadListener(OnReLoadListener onReLoadListener) {
        this.onReLoadListener = onReLoadListener;
    }

    /**
     * 加载xml文件完成
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > 1) {
            throw new IllegalStateException(getClass().getSimpleName() + " can host only one direct child");
        }
        contentView = getChildAt(0);
        if (nowState != CONTENT) {
            contentView.setVisibility(GONE);
            initView();
        }
    }

    /**
     * 显示对应视图
     */
    private void initView() {
        switch (nowState) {
            case LOADING:
                showLoadingView();
                break;
            case EMPTY:
                showEmptyView();
                break;
            case ERROR:
                showErrorView();
                break;
        }
    }

    /**
     * 切换状态时，隐藏正在显示的页面
     */
    private void hideNowView() {
        switch (nowState) {
            case CONTENT:
                contentView.setVisibility(GONE);
                break;
            case LOADING:
                loadingView.setVisibility(GONE);
                break;
            case EMPTY:
                emptyView.setVisibility(GONE);
                break;
            case ERROR:
                errorView.setVisibility(GONE);
                break;
        }
    }

    /**
     * 显示内容页面
     */
    public void showContentView() {
        if (nowState != CONTENT)
        hideNowView();
        contentView.setVisibility(VISIBLE);
        nowState = CONTENT;
    }

    /**
     * 显示加载页面
     */
    public void showLoadingView() {
        if (nowState != LOADING)

            hideNowView();
        if (loadingView == null) {
            loadingView = LayoutInflater.from(getContext()).inflate(R.layout.statelayout_loading, null);
            TextView hintTv = loadingView.findViewById(R.id.tv_loading);
            ImageView imageView = loadingView.findViewById(R.id.imgview);
            Glide.with(getContext()).load(R.drawable.gif_loading).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
            if (!TextUtils.isEmpty(loadingStr))
                hintTv.setText(loadingStr);
            addView(loadingView);
        }
        loadingView.setVisibility(VISIBLE);
        nowState = LOADING;
    }

    /**
     * 显示空数据
     */
    public void showEmptyView() {
        if (nowState != EMPTY)
            hideNowView();
        if (emptyView == null) {
            emptyView = LayoutInflater.from(getContext()).inflate(R.layout.statelayout_empty, null);
            TextView hintTv = emptyView.findViewById(R.id.tv_empty);
            if (!TextUtils.isEmpty(emptyStr))
                hintTv.setText(emptyStr);
            addView(emptyView);
        }
        emptyView.setVisibility(VISIBLE);
        nowState = EMPTY;
    }
    /**
     * 显示加载出错
     */
    public void showErrorView() {
        if (nowState != ERROR)
            hideNowView();
        if (errorView == null) {
            errorView = LayoutInflater.from(getContext()).inflate(R.layout.statelayout_error, null);
            TextView hintTv = errorView.findViewById(R.id.tv_error);
            if (!TextUtils.isEmpty(emptyStr))
                hintTv.setText(emptyStr);
            hintTv.setOnClickListener(v -> {
                showLoadingView();
                if (onReLoadListener != null){
                    onReLoadListener.reLoad();
                }
            });
            addView(errorView);
        }
        errorView.setVisibility(VISIBLE);
        nowState = ERROR;
    }

    public interface OnReLoadListener{
        void reLoad();
    }
}
